import React, {Component} from 'react';
import {
  Layout,
  Menu,
  Breadcrumb,
  Modal,
  Form,
  Input,
  Select,
  DatePicker,
  Popconfirm,
  Card,
  Row,
  Col,
  Button,
  Radio,
  Dropdown,
  Icon,
  Upload,
  message,
  Table,
  Pagination,
  Checkbox
} from 'antd';
const FormItem = Form.Item;
const {Header, Content, Footer} = Layout;
import _ from 'underscore';
import WrapPage from './_wrap'
import styles from './main.less'
import {Link} from 'dva/router';
import * as  CustomerModel from '../model/customer';
import * as InputModel from '../model/input';
import * as  StockModel from '../model/stock';
import * as  ProductModel from '../model/product';
import InputInfo from './component/container_input_info';
import moment from 'moment'
 class _Page extends React.Component {
    constructor(props) {
        super(props);
        this.state = {page:1,pageSize:9999,orderBy:'createAt',filter:{},suitcase_list:[],flag:false};
        this.updateList()
      }
      async updateList(){
           var res = await  StockModel.fetch({
               filter:{components:{$elemMatch:{status:'waitSendInput'}}},
               limit:1000000,
           })
           if(res.status == 'success'){
             let suitcase_map = {}
             res.data.list.map(st=>{
               st.components.map(cp=>{
                 if(cp.suitcase_num){
                   suitcase_map[cp.suitcase_num] = suitcase_map[cp.suitcase_num] || {cnum:cp.suitcase_num,components:[]}
                   if(cp.status==='waitSendInput'){
                     suitcase_map[cp.suitcase_num].components.push(cp)
                   }
                 }
               })
             })
             this.setState({suitcase_list:_.values(suitcase_map)});
           }else{
               message.error('查询失败')
           }
       }
       async sendinput (suitcase){
         console.log('sendinput',{suitcase})
         if(suitcase.components.length<1)return false;
         let input = {suitcase_num:suitcase.cnum,status:'waitInput',components:[]}
         suitcase.components.map(cp=>{
           input.components.push({component_num:cp.cnum,stock_num:cp.cnum.split('-')[0]})
         })
         let res = await InputModel.addItem(input);
         // 接着 更新 到货部件状态
         if(res.data.item){
           let components = res.data.item.components
           for(let cp of components){
             res = await StockModel.getByNum(cp.stock_num)
             let stock = res.data.item
             if(stock){
               for(let scp of stock.components){
                 console.log('check','scp.cnum === cp.stock_num',scp.cnum, cp.component_num,scp.cnum === cp.component_num)
                 if(scp.cnum === cp.component_num){
                   console.log('set input ',stock.cnum,scp.cnum)
                   scp.status = 'waitInput'
                 }
               }
               await StockModel.updateByNum(cp.stock_num,{components:stock.components})
             }
           }
         }
         this.updateList()
       }
  render() {
    console.log("render state",this.state);
          const columns = [
            {
              title: '提箱单号',
              dataIndex: 'cnum',
              key: 'cnum',
              render: text =>
              (<span>{text}</span>),
            },{
              title: '商品数量',
              dataIndex: 'product_quantitys',
              key: 'product_quantitys',
              render: (text,record) =>
              (<span>{record.components.length}</span>),
            },{
              title: '预计到港时间',
              dataIndex: 'plan_arrive_at',
              key: 'plan_arrive_at',
              render: text =>
              (<span>{moment(text).format('YYYY-MM-DD')}</span>),
            },{
              title: '操作',
              dataIndex: 'Operation',
              key: 'Operation',
              render: (text,record) =>
              (<span>
                <InputInfo record={record} >
                  <span style={{cursor:"pointer"}}>查看详情</span>
                </InputInfo>

                <Button type="primary" size='small'
                  onClick={e=>this.sendinput(record)} disabled={record.components.length<1}>发起入库</Button>
              </span>),
            }
          ];
    return (
      <WrapPage>
        <div>
              <Row>
                <Col span={12}>发起入库管理</Col>
                <Col span={12}>
                <Row type="flex" justify="end">
                          <Link to="/inputlist"><Button><Icon type="check-square" />查看入库单</Button></Link>
                    </Row>
                </Col>
              </Row>
              <Table columns={columns} dataSource={this.state.suitcase_list}
                  rowKey={record => record.cnum} pagination={false} scroll={{ x: 1000}}/>

        </div>
      </WrapPage>
    );
  }
}

export default _Page
